package com.xiaoyu.lanling.feature.chat.model.intent

import `in`.srain.cube.request.JsonData
import android.text.TextUtils
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.data.UserInfoDataProvider
import com.xiaoyu.base.entity.ConversationEntity
import com.xiaoyu.base.model.User
import com.xiaoyu.im.ChatToken
import com.xiaoyu.im.datamodel.ConversationType
import com.xiaoyu.im.provider.ConversationManager

class ChatIntentModel {

    lateinit var mChatToken: ChatToken
    var mChatTitle: String? = null
    var mMessageID: String? = null
    var mUser: User? = null
    var mConversationType: String? = null
    var mFromType = ""
    var mExtData: JsonData? = null

    fun setFromType(fromType: String) {
        mFromType = fromType
    }

    fun setMessageId(messageId: String?) {
        mMessageID = messageId
    }

    val messageIDThenReset: String?
        get() {
            val messageId = mMessageID
            mMessageID = ""
            return messageId
        }

    val isSingleNormalChat: Boolean
        get() = isSingleChat && !UserData.getInstance().isSystemAccountUid(mUser?.uid)

    val isSingleChat: Boolean
        get() = TextUtils.equals(mConversationType, ConversationType.SINGLE)

    val isGroupChat: Boolean
        get() = TextUtils.equals(mConversationType, ConversationType.GROUP)

    override fun toString(): String {
        return "ChatIntentModel{" +
                "chatToken=" + mChatToken +
                ", mChatTitle='" + mChatTitle + '\'' +
                ", mUser=" + mUser +
                ", mConversationType='" + mConversationType + '\'' +
                '}'
    }

    companion object {

        fun fromConversationModel(conversation: ConversationEntity): ChatIntentModel {
            val chatIntentModel = ChatIntentModel()
            chatIntentModel.mChatToken = ChatToken.forChatId(conversation.chatId)
            chatIntentModel.mUser = UserInfoDataProvider.getInstance().fetchFromMemory(conversation.toUid)
            chatIntentModel.mExtData = conversation.extData
            if (ConversationManager.getInstance().isSingleChat(conversation)) {
                chatIntentModel.mChatTitle = chatIntentModel.mUser?.remark
            } else {
                chatIntentModel.mChatTitle = conversation.name
            }
            chatIntentModel.mConversationType = conversation.type
            return chatIntentModel
        }

        fun fromUser(user: User): ChatIntentModel {
            val chatIntentModel = ChatIntentModel()
            chatIntentModel.mChatToken = ChatToken.forUid(user.uid)
            chatIntentModel.mUser = user
            chatIntentModel.mChatTitle = user.remark

            val conversation = ConversationManager.getInstance().fetchConversation(chatIntentModel.mChatToken?.chatId)
            chatIntentModel.mExtData = if (conversation == null) JsonData.newMap() else conversation.extData
            chatIntentModel.mConversationType = if (conversation == null) ConversationType.UNKNOWN else ConversationType.SINGLE
            return chatIntentModel
        }
    }
}